<template>
  <!-- <div>123ID:{{$route.params.id}}</div> -->
  <el-card>
    <div slot="header">
       <my-breadcrumb>编辑文章</my-breadcrumb>
    </div>
    <el-form ref="myForm" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="form.coverType" @change="hImageTypeChange">
          <el-radio :label="1" name="type">单图</el-radio>
          <el-radio :label="3" name="type">三图</el-radio>
          <el-radio :label="0" name="type">无图</el-radio>
          <el-radio :label="-1" name="type">自动</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="" v-if="form.coverType>0">
          <el-row>
            <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
                {{idx}}
                <my-cover v-model="form.coverImages[idx]"/>
            </el-col>
          </el-row>
        </el-form-item>
      <my-channel v-model="form.channelId"></my-channel>
      <el-form-item>
        <el-button type="primary" @click="hSave">保存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
// 导入富文本编辑器样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入富文本编辑器组件
import { quillEditor } from 'vue-quill-editor'
export default {
  // 注册富文本编辑器
  components: {
    quillEditor
  },
  name: 'AddArticle',
  data () {
    return {
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      form: {
        title: '', // 标题
        content: '', // 内容
        coverType: 0, // 封面
        coverImages: [], // 保存对应的封面图的地址
        channelId: ''
      },
      // 验证规则
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  // 数据回填
  created () {
    this.loadArticles()
  },
  methods: {
    // 数据回填：获取数据处理函数
    async loadArticles () {
      // 发送请求
      const res = await this.$http({
        url: '/mp/v1_0/articles/' + this.$route.params.id,
        method: 'GET'
      })
      // 成功后赋值给数据项
      console.log(res)
      this.form.title = res.data.data.title
      this.form.content = res.data.data.content
      this.form.coverType = res.data.data.cover.type
      this.form.coverImages = res.data.data.cover.images
      this.form.channelId = res.data.data.channel_id
    },
    hImageTypeChange () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType === 3) {
        this.form.coverImages.length = 3
      } else {
        this.form.coverImages.length = 0
      }
    },
    // 点击保存的处理函数
    hSave () { // draft确定是发布还是草稿
    // 表单验证
      this.$refs.myForm.validate(valid => {
        if (valid) {
          this.doave()
        }
      })
    },
    // 保存处理函数
    async doave () {
      try {
        // 结构赋值
        const { title, content, channelId, coverType, coverImages } = this.form
        // 调接口
        const res = await this.$http({
          method: 'PUT',
          url: '/mp/v1_0/articles/' + this.$route.params.id,
          data: {
            title, // 标题
            content, // 内容
            channel_id: channelId,
            cover: {
              type: coverType, // 无图参数
              images: coverImages // 有图参数
            }
          }
        })
        console.log(res)
        // 发布成功后提示
        this.$message.success('保存成功')
      } catch (err) {
      // 错误提示
        console.log(err)
        this.$message.error('保存失败')
      }
    }
  }
}
</script>

<style lang='less' scoped>
 /deep/ .ql-container{
  height: 200px;
}
 /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
